/**
 * icheck - Directive for custom checkbox icheck
 */
class icheck{
    constructor($timeout) {
        this.$timeout = $timeout;
        this.restrict = 'A';
        this.require  = 'ngModel';
    }

    link(scope, element, attrs, ngModel) {
        // return this.$timeout( () => {
            var value;
            value = attrs['value'];

            scope.$watch(attrs['ngModel'], (newValue) => {
                $(element).iCheck('update');
            })

            return $(element).iCheck({
                checkboxClass: 'icheckbox_square-green',
                radioClass: 'iradio_square-green'

            }).on('ifChanged', (event) => {
                    if ($(element).attr('type') === 'checkbox' && attrs['ngModel']) {
                        scope.$apply( () => {
                            return ngModel.$setViewValue(event.target.checked);
                        });
                    }
                    if ($(element).attr('type') === 'radio' && attrs['ngModel']) {
                        return scope.$apply( () => {
                            return ngModel.$setViewValue(value);
                        });
                    }
                });
        // });
    }
}

icheck.$inject = ['$rootScope', '$timeout'];

export default icheck;